package com.qingmi.farmer.ui.buy;

import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;

import com.qingmi.farmer.data.model.GoodsInfo;
import com.qingmi.farmer.data.model.Param;
import com.qingmi.farmer.data.model.Supplier;
import com.wjjun.base.ui.base.BaseActivityViewModel;
import com.qingmi.farmer.viewmodel.ParamItemViewModel;
import com.wjjun.viewmodel.BannerViewModel;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * 商品详情的ViewModel
 */
public class GoodsDetailViewModel extends BaseActivityViewModel {

    public String id;

    public GoodsInfo goodsInfo;

    public Supplier supplier;


    //数据源
    public final ObservableField<ObservableList> items = new ObservableField<>();

    //绑定关系,多类型item就是通过这个实现的
    public final ObservableField<ItemBinding> itemBinding = new ObservableField<>();

    public final ObservableList<ParamItemViewModel> paramList = new ObservableArrayList<>();

    public final ObservableField<BannerViewModel> bannerViewModel = new ObservableField<>(new BannerViewModel());


    //图标
    public final ObservableField<String> icon = new ObservableField<>();

    //商品名称
    public final ObservableField<String> goodsName = new ObservableField<>();

    //实际单价
    public final ObservableField<Double> price = new ObservableField<>(0.0);

    //购买数量
    public final ObservableField<Integer> count = new ObservableField<>(0);

    public final ObservableField<String> unit = new ObservableField<>();

    //销量
    public final ObservableField<Integer> sellCount = new ObservableField<>(0);

    //存库
    public final ObservableField<Integer> inventory = new ObservableField<>(0);

    //供应商名字
    public final ObservableField<String> suppliersName = new ObservableField<>();

    //管局信息
    public final ObservableField<String> superviseName = new ObservableField<>();

    //总价
    public final ObservableField<Double> totalPrice = new ObservableField<>(0.0);

    //图文详情
    public final ObservableField<String> descUrl = new ObservableField<>();

    //分页控制
    public final ObservableField<Boolean> paramTab = new ObservableField<>(true);

    public final ObservableField<Boolean> detailTab = new ObservableField<>(false);



    public void bindData(GoodsInfo goodsInfo) {
        items.set(paramList);

        this.goodsInfo = goodsInfo;

        id = goodsInfo.getGoodId();
        goodsName.set(goodsInfo.getName());
        supplier = goodsInfo.getSupplier();
        superviseName.set(goodsInfo.getSuperviseName());
        icon.set(goodsInfo.getPic());

        price.set(goodsInfo.getPrice());
        unit.set(goodsInfo.getUnit());

        if (goodsInfo.getSupplier() != null) {
            suppliersName.set(goodsInfo.getSupplier().getSupplierName());
        }
        sellCount.set(goodsInfo.getSales());
        inventory.set(goodsInfo.getAmount());
        descUrl.set(goodsInfo.getDescUrl());

        if (goodsInfo.getSpecificationParaJson() != null && !goodsInfo.getSpecificationParaJson().isEmpty()) {
            for (Param item : goodsInfo.getSpecificationParaJson()) {
                paramList.add(new ParamItemViewModel(item));
            }
        }


    }
}
